package com.arcsinw.bingeveryday;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class JsonHelper {

    public static Wallpaper toWallpaper(String json){
        Wallpaper wallpaper = new Wallpaper();
        try {
            JSONObject jsonObject = new JSONObject(json);
            wallpaper.setStartDate(jsonObject.getString("startdate"));
            wallpaper.setFullStartDate(jsonObject.getString("fullstartdate"));
            wallpaper.setEndDate(jsonObject.getString("enddate"));
            wallpaper.setUrl(jsonObject.getString("url"));
            wallpaper.setUrlBase(jsonObject.getString("urlbase"));
            wallpaper.setCopyRight(jsonObject.getString("copyright"));
            wallpaper.setCopyRightLink(jsonObject.getString("copyrightlink"));
            wallpaper.setTitle(jsonObject.getString("title"));
            wallpaper.setQuiz(jsonObject.getString("quiz"));
            wallpaper.setWp(jsonObject.getBoolean("wp"));

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return wallpaper;
    }

    public static Wallpaper toWallpaper(JSONObject jsonObject){
        Wallpaper wallpaper = new Wallpaper();
        try {
            wallpaper.setStartDate(jsonObject.getString("startdate"));
            wallpaper.setFullStartDate(jsonObject.getString("fullstartdate"));
            wallpaper.setEndDate(jsonObject.getString("enddate"));
            wallpaper.setUrl(jsonObject.getString("url"));
            wallpaper.setUrlBase(jsonObject.getString("urlbase"));
            wallpaper.setCopyRight(jsonObject.getString("copyright"));
            wallpaper.setCopyRightLink(jsonObject.getString("copyrightlink"));
            wallpaper.setTitle(jsonObject.getString("title"));
            wallpaper.setQuiz(jsonObject.getString("quiz"));
            wallpaper.setWp(jsonObject.getBoolean("wp"));

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return wallpaper;
    }

    public static ArrayList<Wallpaper> toWallpapers(String json){
        ArrayList<Wallpaper> wallpapers = new ArrayList<Wallpaper>();

        JSONArray images = new JSONArray();
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(json);
            images = jsonObject.getJSONArray("images");

            for(int i = 0; i < images.length(); i++)
            {
                JSONObject wallpaper = images.getJSONObject(i);
                wallpapers.add(toWallpaper(wallpaper));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return wallpapers;
    }
}
